package zhao.tx.eecs.Service;

import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo.State;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import zhao.tx.eecs.JsonModel.LocationModel;
import zhao.tx.eecs.myThread.WatchDog;
import zhao.tx.eecs.myThread.WatchdogInterface;

public class WatchdogService extends Service{

	private IBinder binder=new LocalBinder();

	private volatile static WatchDog SINGLE_THREAD ;


	private static boolean isSingleStart = false;

	private WatchDog watchDog = null;
	
	private Context context;

	public static WatchDog getSingleThread () {
		if ( SINGLE_THREAD == null ) {
			synchronized (WatchDog.class) {
				if ( SINGLE_THREAD == null ) {
					SINGLE_THREAD = new WatchDog( 1*1000, 6*1000 );
				}
			}
		}
		if ( isSingleStart ) {
			SINGLE_THREAD.setSatrt(true);
		} 
		return SINGLE_THREAD;
	}



	/* （非 Javadoc）
	 * @see android.app.Service#onBind(android.content.Intent)
	 */
	@Override
	public IBinder onBind(Intent intent) {
		// TODO 自动生成的方法存根
		return binder;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		context = getApplicationContext();

	}

	private WatchdogInterface iThreadInterface = new WatchdogInterface () {

		@Override
		public void thraadDoSomehing() {
			// TODO Auto-generated method stub

		}

		@Override
		public void stopDo() {
			// TODO Auto-generated method stub

		}

		@Override
		public void timeGone() {
			// TODO Auto-generated method stub
			Intent intent = new Intent ( WatchdogService.this,LocationService.class );
			intent.putExtra("isFirst", false);
			startService(intent);
		}
	};

	private void startAlarmService () {
		
		Log.i("test", "startAlarm");
		AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
		Calendar calendar = Calendar.getInstance();  
		calendar.setTimeInMillis(System.currentTimeMillis());  
		calendar.add(Calendar.SECOND, 30);  //设置30秒后执行
		Intent intent = new Intent(context,WatchdogService.class);
		intent.putExtra("isAlarm", true);
		/* 
		 * 根据PendingIntent.getBroadcast 
		 * 指明定时唤醒广播接收者 
		 * */  
		PendingIntent pending = PendingIntent.getService(context,   
				0,intent, 0);
		am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending); 
	}

	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		if ( intent != null ) {
			boolean isAlarm = intent.getBooleanExtra("isAlarm", false);
			
			if ( isAlarm ) {
				startAlarmService();
			}
		}
		Log.i("isDog", "true");
		watchDog = getSingleThread();
		watchDog.setiThreadInterface(iThreadInterface);
		if ( watchDog.isSatrt() ) {
			
			watchDog.suspendThread();

		} 
		if ( watchDog.isSatrt() || watchDog.isAlive() ) {
			watchDog.resumeThread();
		} else {
			isSingleStart = true;
			watchDog.start();	
		}
		
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		return super.onStartCommand(intent, flags, startId);
	}

	//定义内容类继承Binder
	public class LocalBinder extends Binder{
		//返回本地服务
		WatchdogService getService(){
			return WatchdogService.this;
		}
	}

}
